﻿using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using AutoMapper;
using O.M.Production.Application.IService;
using O.M.Production.Domain.DTO.Produce_work_order;
using O.M.Production.Domain.DTO.quality_inspection_management;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using O.M.Production.Infrastuecure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace O.M.Production.Application.Service
{
    /// <summary>
    /// 质检管理服务接口实现类
    /// </summary>
    public class quality_inspection_management_Service : BaseServices<quality_inspection_management, string>, I_quality_inspection_management_Service
    {
        private readonly I_produce_work_order_Repository _produce_work_order_Repository;
        private readonly IMapper _mapper;
        private readonly I_quality_inspection_management_Repository _quality_Inspection_Management_Repository;
        private readonly I_quality_inspection_report_Repository _quality_Inspection_Report_Repository;
        public quality_inspection_management_Service(I_quality_inspection_management_Repository quality_Inspection_Management_Repository, IMapper mapper, I_quality_inspection_report_Repository quality_Inspection_Report_Repository, I_produce_work_order_Repository produce_work_order_Repository) : base(quality_Inspection_Management_Repository)
        {
            _quality_Inspection_Management_Repository = quality_Inspection_Management_Repository;
            _mapper = mapper;
            _quality_Inspection_Report_Repository = quality_Inspection_Report_Repository;
            _produce_work_order_Repository = produce_work_order_Repository;
        }



        /// <summary>
        /// 分页查询-质检管理
        /// </summary>
        /// <param name="QIMId_Name">编号或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Get_quality_inspection_management(string? QIMId_Name, int pageSize, int pageIndex)
        {
            try
            {
                ApiResult apiResult = new ApiResult();
                Expression<Func<quality_inspection_management, bool>> where = i => true;
                if (!string.IsNullOrEmpty(QIMId_Name))
                {
                    where = i => i.quality_inspection_work_order_Id.Contains(QIMId_Name) || i.quality_inspection_work_order_name.Contains(QIMId_Name);
                }
                var list = await _quality_Inspection_Management_Repository.GetShowQueryableAsync(pageSize, pageIndex, where);
                apiResult.Data = list.Data.OrderByDescending(i => i._add_time).ToList();
                apiResult.totalCount = list.TotalCount;
                apiResult.pageCount = list.PageCount;
                return apiResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 返回单个实体-质检管理
        /// </summary>
        /// <param name="id">质检编号</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Get_quality_inspection_management_Model(string id)
        {
            var quality_inspection_management = await _quality_Inspection_Management_Repository.GetShowObjAsync(x => x.quality_inspection_work_order_Id == id);
            var quality_inspection_management_DTO = _mapper.Map<quality_inspection_management_DTO>(quality_inspection_management);
            var quality_Inspection_Report = await _quality_Inspection_Report_Repository.GetShowInforAsync(x => x.quality_inspection_work_order_Id == id);
            if (quality_Inspection_Report.Count != 0)
            {
                quality_inspection_management_DTO.quality_Inspection_Report_DTOs = _mapper.Map<List<quality_inspection_report_DTO>>(quality_Inspection_Report);
            }
            ApiResult apiResult = new ApiResult();
            if (quality_inspection_management_DTO == null)
            {
                apiResult.Code = 500;
                apiResult.Message = "未找到该质检管理信息";
            }
            else
            {
                apiResult.Code = 200;
                apiResult.Message = "获取成功";
                apiResult.Data = quality_inspection_management_DTO;
            }
            return apiResult;
        }
        /// <summary>
        /// 修改-质检管理
        /// </summary>
        /// <param name="quality">质检管理实体</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Put_Upd_quality_inspection_management(quality_inspection_management quality)
        {
            try
            {
                ApiResult apiResult = new ApiResult();
                _quality_Inspection_Management_Repository.BeginTranInfor();
                var res = await _quality_Inspection_Management_Repository.UpdInfor_Async(quality);
                if (res > 0)
                {
                    apiResult.Code = 200;
                    apiResult.Message = "修改成功";
                    _quality_Inspection_Management_Repository.CommitTranInfor();
                }
                else
                {
                    apiResult.Code = 500;
                    apiResult.Message = "修改失败";
                    _quality_Inspection_Management_Repository.RollbackTranInfor();
                }

                return apiResult;

            }
            catch (Exception)
            {
                _quality_Inspection_Management_Repository.RollbackTranInfor();
                throw;
            }
        }
        /// <summary>
        /// 质检-生产工单
        /// </summary>
        /// <param name="queality_Testing_DTO">质检DTO</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Post__quality_testing(Queality_Testing_DTO queality_Testing_DTO)
        {
            try
            {
                _quality_Inspection_Report_Repository.BeginTranInfor();
                var obj = await _produce_work_order_Repository.GetShowObjAsync(x => x.work_order_id == queality_Testing_DTO.quality_inspection_work_order_Id);
                var obj2 = await _quality_Inspection_Management_Repository.GetShowObjAsync(x => x.quality_inspection_work_order_Id == queality_Testing_DTO.quality_inspection_work_order_Id);
                obj2.quality_inspection_times += 1;
                obj.qualified_quantity += queality_Testing_DTO.qualified_quantity;
                obj.bad_quantity += queality_Testing_DTO.unqualified_quantity;
                var res = await _produce_work_order_Repository.UpdInfor_Async(obj);
                var res2 = await _quality_Inspection_Management_Repository.UpdInfor_Async(obj2);
                ApiResult apiResult = new ApiResult();
                if (res > 0 && res2 > 0)
                {
                    apiResult.Code = 200;
                    apiResult.Message = "质检成功";
                    _quality_Inspection_Report_Repository.CommitTranInfor();
                }
                else
                {
                    apiResult.Code = 500;
                    apiResult.Message = "质检失败";
                    _quality_Inspection_Report_Repository.RollbackTranInfor();
                }
                return apiResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                _quality_Inspection_Report_Repository.RollbackTranInfor();
                throw;
            }
        }
    }
}
